<?php

class Core_Model_DbTable_InputPhotos extends Zend_Db_Table_Abstract {

    protected $_name = 'input_photos';

    public function getByGIID($id) {
        $cond = $this->getAdapter()->quoteInto("gi_id = (?)", $id);
        $docs = $this->fetchAll($cond)->toArray();
        foreach ($docs as &$doc) {
            $splits = explode("/", $doc['file']);
            $file_name = end($splits);
            $doc['file_name'] = $file_name;
            $splits_type = explode(".", $file_name);
            $file_extension = end($splits_type);
            switch ($file_extension) {
                case 'jpg':
                case 'png':
                case 'gif':
                    $doc['file_type'] = 'bmp';
                    break;
                case 'xlsx':
                case 'xls':
                    $doc['file_type'] = 'excel';
                    break;
                case 'pdf':
                    $doc['file_type'] = 'pdf';
                    break;
                default:
                    $doc['file_type'] = 'word';
                    break;
            }
        }
        return $docs;
    }
    
    public function getByFolder($folder_id)
    {
        $cond = $this->getAdapter()->quoteInto("folder = (?)", $folder_id);
        $docs = $this->fetchAll($cond)->toArray();
        foreach ($docs as &$doc) {
            $splits = explode("/", $doc['file']);
            $file_name = end($splits);
            $doc['file_name'] = $file_name;
            $splits_type = explode(".", $file_name);
            $file_extension = end($splits_type);
            switch ($file_extension) {
                case 'jpg':
                case 'png':
                case 'gif':
                    $doc['file_type'] = 'bmp';
                    break;
                case 'xlsx':
                case 'xls':
                    $doc['file_type'] = 'excel';
                    break;
                case 'pdf':
                    $doc['file_type'] = 'pdf';
                    break;
                default:
                    $doc['file_type'] = 'word';
                    break;
            }
        }
        return $docs;
    }
    
    public function deleteByFolder($folder_id)
    {
        $photos = $this->getByFolder($folder_id);
        foreach ($photos as $photo)
        {
            $full_file_name = UPLOAD_PATH . $photo['file'];
            unlink($full_file_name);
        }
        $this->delete("folder = $folder_id");
    }

}

?>
